package com.showde.shoppingmall.db;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import com.kim.androidbase.db.DBCommandInteface;
import com.kim.androidbase.db.DBSession;
import com.kim.androidbase.db.IDBUtils.QueryCompleteCallback;
import com.kim.androidbase.db.SQLiteCriteria;
import com.showde.shoppingmall.db.constant.ProductConstant;
import com.showde.shoppingmall.domain.Product;

public class ProductDBCommand implements DBCommandInteface<Product, String> {

	private static final String TAG = "ProductDBCommand";

	/**
	 * 广告商品
	 */
	public static final String TYPE_ADVERTISEMENT = "1";
	/**
	 * 热门商品
	 */
	public static final String TYPE_HOT = "1";

	DBSession dbSession;

	public ProductDBCommand(Context context) {
		dbSession = new DBSession(context, new DBHelper(context));
	}

	@Override
	public void add(Product product) {
		dbSession.insert(ProductConstant.TABLENAME,
				getProductContentValue(product),
				SQLiteDatabase.CONFLICT_REPLACE);
	}

	@Override
	public void add(List<Product> products) {

		List<ContentValues> contentValuesList = new ArrayList<ContentValues>();
		for (Product product : products) {
			contentValuesList.add(getProductContentValue(product));
		}
		dbSession.insertBatch(ProductConstant.TABLENAME, contentValuesList,
				SQLiteDatabase.CONFLICT_REPLACE);

	}

	@Override
	public void delete(String pk) {

	}

	@Override
	public void delete(SQLiteCriteria sqLiteCriteria) {

	}

	@Override
	public void modify(SQLiteCriteria sqLiteCriteria) {

	}

	@Override
	public void modify(String pk, SQLiteCriteria sqLiteCriteria) {

	}

	@Override
	public Product query(final String PK) {
		final Product product = new Product();
		dbSession.query(ProductConstant.TABLENAME, ProductConstant.dbColumns,
				ProductConstant.PRODUCTID + "=?", new String[] { PK }, null,
				null, null, new QueryCompleteCallback() {

					@Override
					public void onQueryError(String msg) {
						Log.d(TAG, String.format("query[%s]", PK));
					}

					@Override
					public void onQueryComplete(Cursor cursor) {
						if (cursor != null && cursor.getCount() > 0) {
							while (cursor.moveToNext()) {
								getProductFromCursor(product, cursor);
							}
						}
					}
				});
		return product;
	}

	@Override
	public List<Product> query(SQLiteCriteria sqLiteCriteria) {
		return null;
	}

	@Override
	public List<Product> queryAll() {
		// TODO Auto-generated method stub
		return null;
	}

	/**
	 * 根据商品类别ID获取商品列表
	 * 
	 * @param productCategoryId
	 * @return
	 */
	public List<Product> queryProductByCategoryId(final String productCategoryId) {

		final List<Product> products = new ArrayList<Product>();

		dbSession.query("select * from " + ProductConstant.TABLENAME
				+ " where " + ProductConstant.PRODUCTCATEGORYID + " =?",
				new String[] { productCategoryId },
				new QueryCompleteCallback() {

					@Override
					public void onQueryError(String msg) {
						Log.d(TAG, String.format(
								"queryProductByCategoryId[%s]:%s",
								productCategoryId, msg));
					}

					@Override
					public void onQueryComplete(Cursor cursor) {
						if (cursor != null && cursor.getCount() > 0) {
							Product product = null;
							while (cursor.moveToNext()) {
								product = new Product();
								getProductFromCursor(product, cursor);
								products.add(product);
							}
						}
					}
				});

		return products;
	}

	/**
	 * 按照商品类型获取商品信息，如热门，推荐，人气...
	 * 
	 * @param productCategoryId
	 * @return
	 */
	public List<Product> queryProductByType(final String productType) {

		final List<Product> products = new ArrayList<Product>();

		dbSession.query("select * from " + ProductConstant.TABLENAME
				+ " where " + ProductConstant.PRODUCTCATEGORYID + " =?",
				new String[] { productType }, new QueryCompleteCallback() {

					@Override
					public void onQueryError(String msg) {
						Log.d(TAG, String.format("queryProductByType[%s]:%s",
								productType, msg));
					}

					@Override
					public void onQueryComplete(Cursor cursor) {
						if (cursor != null && cursor.getCount() > 0) {
							Product product = null;
							while (cursor.moveToNext()) {
								product = new Product();
								getProductFromCursor(product, cursor);
								products.add(product);
							}
						}
					}
				});

		return products;
	}

	/**
	 * 获取商品ContentValue
	 * 
	 * @param product
	 * @return
	 */
	private ContentValues getProductContentValue(Product product) {
		ContentValues contentValues = new ContentValues();
		contentValues.put(ProductConstant.PRODUCTID, product.getProductID());
		contentValues
				.put(ProductConstant.PRODUCTNAME, product.getProductName());
		contentValues.put(ProductConstant.PRODUCTTITLE,
				product.getProductTitle());
		contentValues
				.put(ProductConstant.PRODUCTLOGO, product.getProductLogo());
		contentValues.put(ProductConstant.PRODUCTPRICE,
				product.getProductPrice());
		contentValues.put(ProductConstant.PRODUCTCATEGORYID,
				product.getProductCategoryID());
		return contentValues;
	}

	private void getProductFromCursor(Product product, Cursor cursor) {
		product.setProductID(dbSession.getString(cursor,
				ProductConstant.PRODUCTID));
		product.setProductName(dbSession.getString(cursor,
				ProductConstant.PRODUCTNAME));
		product.setProductTitle(dbSession.getString(cursor,
				ProductConstant.PRODUCTTITLE));
		product.setProductLogo(dbSession.getString(cursor,
				ProductConstant.PRODUCTLOGO));
		product.setProductPrice(dbSession.getFloat(cursor,
				ProductConstant.PRODUCTPRICE));
		product.setProductCategoryID(dbSession.getString(cursor,
				ProductConstant.PRODUCTCATEGORYID));
	}

}
